-- 万国觉醒
-- main.lua  

-- Create By TouchSpriteStudio on 13:31:43   
-- Copyright © TouchSpriteStudio . All rights reserved.
--unlockDevice()
-- require("TSLib")
--require("tsp")

	
__UI = {}
_UI = {}
require('api')
require('function')
require('tsp')
require('ui')


init(1)
_app = {}
_app.bid = 'com.lilithgames.rok.ios.offical'




t={}
degree = 80

t['游戏主界面'] = { 0x2bbbe9, "0|0|0x2bbbe9,28|1|0xffef00", degree, 4, 2, 82, 22 }
	t['游戏主界面-野外'] = { 0xb26c10, "0|0|0xb26c10,-408|639|0x0b5582,-407|647|0x78e5ff,-441|-17|0x5ef0f7", degree, 14, 0, 503, 705 }
	t['游戏主界面-城内'] = { 0x02527c, "0|0|0x02527c,7|-18|0x2179a7,7|-2|0x71e8ff,1|-133|0xffffff,-2|-104|0x004b7b", degree, 36, 517, 90, 684 }


function _init()
    if d('游戏主界面-野外',false,1,2) then
		log('初始化完成')
		return true
    else
        d('游戏主界面-城内',true,2)
	end
end

function showLog(txt)
	adbox = adbox or 0
	if adbox == 0 then
		adbox = 1
		--创建文本视图
		fwShowWnd("window1",95,8,270,34,1);
		mSleep(500)
	end
	fwShowTextView("window1","text1",txt,"center","FF0000","FFDAB9",10,0,0,0,170,26,0.8);
	--fwShowTextView("window1","text1","文本视图","center","FF0000","FFDAB9",10,0,0,0,200,30,0.5);
end


t['弹窗-战争准备'] = { 0xb67016, "0|0|0xb67016,3|-2|0xb67016,78|-2|0xffb200,-77|-1|0xffb100", degree, 541, 574, 737, 627 }
t['弹窗-头衔x'] = { 0xd5d2cb, "0|0|0xd5d2cb,-1|-13|0xb9b7aa,-15|1|0xbab7aa,7|37|0x044461", 95, 1050, 38, 1139, 140 }
t['弹窗-邮件x'] = { 0xd3d1ca, "0|0|0xd3d1ca,9|-2|0x9b9a8a,17|-2|0xb6b3aa", 95, 1212, 24, 1264, 82 }
t['弹窗-返回'] = { 0xd7d5cf, "0|0|0xd7d5cf,12|1|0xb2aca5,10|13|0xcfcfcf,11|17|0x8e8e86", degree, 15, 13, 67, 63 }
--设置这里为弹窗处理中心
function _Evevnt()
    _Evevnt_arr = { '弹窗-返回', '弹窗-战争准备', '弹窗-头衔x', '弹窗-邮件x',  }
    for k,v in ipairs(_Evevnt_arr) do
        if d(v,true,1,2) then
            return true
        end
    end
end


t['头衔-搜索'] = { 0xf5f5f4, "0|0|0xf5f5f4,35|-5|0xb26c10", degree, 424, 1, 495, 33 }
    t['头衔-搜索-搜索'] = { 0x00c8ff, "0|0|0x00c8ff,9|-2|0x117cb9,15|11|0x117cb9", degree, 860, 121, 908, 163 }
    t['头衔-帐号信息面板'] = { 0x1275b7, "0|0|0x1275b7,9|-7|0x1177bb,8|1|0x45cdef,8|6|0x1273ba,-56|0|0x9a5200,-301|-9|0xd8dacf", degree, 0, 0, 1195, 638 }
    t['头衔-头衔冠'] = { 0x995500, "0|0|0x995500,11|-4|0xffa600,404|-1|0x1177bb,404|13|0x1374ba", degree, 0, 32, 1195, 638 }
        t['头衔-头衔冠-确定'] = { 0x2385c4, "0|0|0x2385c4,5|-4|0x1274ba,21|-5|0x00c9ff", degree, 564, 617, 718, 662 }

function title()
    titleKey = false
    showLog( __game.info.x..":"..__game.info.y..__game.info.title  ) 
    if d("头衔-搜索-搜索") or d('头衔-搜索',true,1,2) then
        
        --点，准备输入国家
        click(442,141,2)
        input('\b\b\b\b\b\b\b\b\b')
        
        if __game.info.country_lost and #__game.info.country_lost > 4 then
            input(__game.info.country_lost)
            delay(1)
        else
            input(__game.info.country)
        end
        click(1176,659,1)       --安卓的确定
        
        click(624, 142,1)    --点击x
        input(__game.info.x)
        delay(1)
        click(1176,659,1)       --安卓的确定
        
        click(789, 142,1)    --点击y
        input(__game.info.y)
        delay(1)
        click(1176,659,1)       --安卓的确定
        
        d("头衔-搜索-搜索",true,1,5)

        log("点屏中间")
        local fourclick = {
            {hight/2,width/2,},
            {hight/2,width/2-60,},
            {hight/2+60,width/2,},
            {hight/2,width/2+60,},
            {hight/2-60,width/2,},
        }
        
        for i = 1,5 do
            log( {fourclick[i][1],fourclick[i][2]} )
            click( fourclick[i][1],fourclick[i][2],1 )
            if d("头衔-帐号信息面板") then
                --发现了帐号信息面板 准备截图
                break
            end
        end
   
        
        -- if d("头衔-帐号信息面板") then
        --     --发现了帐号信息面板 准备截图
        --     uploadname(x-290,y+40,x-60,y+70)
        -- end

        if (d("头衔-头衔冠",true,1,2)) then
            
            local Ti = { ['公爵']={521, 396, 0x002132},['大建筑师']={749, 396, 0x002032},['大科学家']={979, 394, 0x002032}}
    
            if isColor(Ti[__game.info.title][1],Ti[__game.info.title][2],Ti[__game.info.title][3],90) then
                click(Ti[__game.info.title][1],Ti[__game.info.title][2],1 )
            end
            
            local i = 0
            while i < 5 and d("头衔-头衔冠-确定",true,1,2) do
                i=i+1
                titleKey = true
            end
            if titleKey and not( d("头衔-头衔冠-确定") ) then
                log('执行成功')
                return true
            end
            
        else
            log('没有找到-头衔冠')
        end
        
    end
end


--取任务
function getKingTask()

    log('准备任务')
	local ts = require("ts")
	local json = ts.json

    local url = 'https://rokapi.wenfree.cn/';
    local arr = {}
    arr['s']='App.RokTaskNew.Get'
    arr['country'] = UIv.country
    log( arr )
    
	local account_ = post(url,arr)
	log(account_)
	if account_.data  == '暂无任务' then
	    return false
	else
    	__game.info = account_.data
    	return true
	end
end

--发头衔
function __titlegame()

    local _event_times = 0
    local search_times = 0
	local timeline = os.time()
	while os.time()-timeline < 60 * 5 do
		if active(_app.bid,5)then
			if _init() then
			    --如果搜索，2次，也没有找到，暂时跳过
                search_times = search_times + 1
                if search_times > 2 then
                    getKingTaskBack()
                    return true
                end
                
                --如果是在2次内的，就去发头衔
                local titleres = title()
                if  titleres  then
                    getKingTaskBack()
                    return true
                end
			else
			    if not( _Evevnt() ) then
			        _event_times = _event_times +1
			        if _event_times > 3 then
			            click(40,40,2)
			        end
			    end
			end
		end
		delay(1)
	end
	return 'next'
end


	
-- delay(1)
-- d("弹窗-头衔x",true,1,2)
-- log({x,y})
-- lua_exit()




lcoalsBid = {"国服","国际服","台服"}

local bidarr = {}
bidarr['国际服'] = "com.lilithgame.roc.ios"
bidarr['国服'] = "com.lilithgames.rok.offical.cn"
bidarr['台服'] = "com.lilithgame.roc.ios.tw"

bid = bidarr[lcoalsBid[UIv.locals+1]]
_app.bid = bid


function main()
    
    __game = {}

	while true do
        if getKingTask() then
            --log( __game.info )
            __titlegame()
            --清理帐号
            -- clearOneAccount();
            delay(1)
            
        else
            delay(5)
            pressHomeKey(0)
            pressHomeKey(1)
        end
	end
	
end


if not(__reg)  then
    while (true) do
    	local ret,errMessage = pcall(main)
    	if ret then
    	else
    	    vpnx();
    		log(errMessage)
    		dialog(errMessage, 10)
    		mSleep(2000)
    	end
    end
end














	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	